import logging
import time
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.common.exceptions import TimeoutException, NoSuchElementException
from Base.driver import Driver


class Base:

    def __init__(self, tag):
        """初始化driver"""
        if tag == "app":
            self.driver = Driver.get_app_driver()
        if tag == "mp":
            self.driver = Driver.get_mp_driver()
        if tag == "mis":
            self.driver = Driver.get_mis_driver()

    def search_ele(self, loc, timeout=10, poll=1.0):
        """
        定位单个元素
        :param loc: 元组（）
        :param timeout: 超时时间
        :param poll: 搜索时间间隔
        :return: 定位对象

        """
        logging.info("操作单个元素：{}".format(loc))
        return WebDriverWait(self.driver, timeout, poll).until(lambda x: x.find_element(loc[0], loc[1]))

    def search_eles(self, loc, timeout=10, poll=1.0):
        """定位多个元素"""
        logging.info("操作一组元素：{}".format(loc))
        return WebDriverWait(self.driver, timeout, poll).until(lambda x: x.find_elements(loc[0], loc[1]))

    def search_click(self, loc, timeout=10, poll=1.0):
        """点击单个元素"""
        logging.info("点击操作")
        self.search_ele(loc, timeout, poll).click()

    def input_ele(self, loc, text, timeout=10, poll=1.0):
        """输入文本"""
        logging.info("输入操作")
        # 定位文本框
        ele = self.search_ele(loc, timeout, poll)
        # 清除文本框
        ele.clear()
        # 输入文本
        ele.send_keys(text)
        logging.info("输入文本：{}".format(text))

    def page_contains_text(self, text, tag="web"):
        """

        :param text: 查找文本
        :return:
        """
        logging.info("查找页面文本：{}".format(text))
        text_xpath = None
        if tag == 'web':
            text_xpath = (By.XPATH, "//*[contains(text(),'{}')]".format(text))
        elif tag == 'app':
            text_xpath = (By.XPATH, "//*[contains(@text,'{}')]".format(text))

        try:
            self.search_ele(text_xpath)
            logging.info("查找页面文本：{}".format(text))
            return True
        except TimeoutException as e:
            logging.info("查找页面文本：{}不存在".format(text))
            print("元素没有定位到：{}".format(e))

            return False

    def select_option_name(self, select, option, name):
        """
        选择下拉框选项
        :param select:下拉框按钮定位方式
        :param option:下拉框选项
        :param name: 下拉框名字
        :return:
        """
        logging.info("下拉框选择的是：{}".format(name))
        # 点击频道
        self.search_click(select)
        time.sleep(1)
        # 定位下拉框所有选项
        eles = self.search_eles(option)
        # 加一个变量标示是否找到元素
        is_element = False
        for i in eles:
            if i.text == name:
                i.click()
                logging.info("下拉框：{}找到".format(name))
                # 找到变为True
                is_element = True
                # 跳出循环
                break
            # 当前元素  向下移动
            ActionChains(self.driver).move_to_element(i).send_keys(Keys.DOWN)

        if not is_element:
            ("下拉框：{}未找到".format(name))
            # 找不到元素异常
            raise NoSuchElementException("下拉框文本：{}未找到".format(name))
